package DEEPERsource.DEEPERsource.source.machinelearning.features;

import java.util.ArrayList;
import java.util.Enumeration;
import java.util.HashMap;
import java.util.HashSet;
import java.util.List;
import java.util.Map;
import java.util.Set;

import treedata.types.GrammarRole;
import treedata.types.PennPOS;
import weka.core.Attribute;
import weka.core.FastVector;
import weka.core.Instance;
import weka.core.Instances;
import deeper.Interaction;
import edu.uci.ics.jung.graph.Edge;
import edu.uci.ics.jung.graph.Vertex;
import graph.Keys;

/**
 * Feature builder that takes into account POS in parse path. 
 * @author tfayruzo
 *
 */
public class DepFeatureBuilder implements FeatureBuilder {
	
	private List<Attribute> structure;
	private Instances data;
		
	public DepFeatureBuilder(){
		structure = new ArrayList<Attribute>();
		for(GrammarRole pos : GrammarRole.values())
			structure.add(new Attribute(pos.name()));
		//structure.add(new Attribute("length"));
		FastVector  v = new FastVector(); 
		for(Attribute a : structure)
			v.addElement(a);
		data = new Instances("",v,1);
	}

	/**
	 * Returns a numeric feature vector, that counts POS
	 * that occur in shortest parse path  
	 * @param i
	 * @return
	 */
	public Instance getFeatureVector(Interaction i) {
		Instance inst = new Instance(structure.size());
		Map<String, Integer> posMap = new HashMap<String, Integer>();
		if(i.depPath!=null){			
			for(Edge e : i.depPath.edges){
				String key = (String)e.getUserDatum(Keys.LABEL);
				Integer count = posMap.get(key);
				if(count == null)
					count = new Integer(0);
				posMap.put(key, count+1);
			}
		}
		for(Enumeration<Attribute> e = data.enumerateAttributes(); e.hasMoreElements();){
			Attribute a = e.nextElement();
			//if(a.name().equals("length")&&i.depPath!=null){
			//	inst.setValue(a, i.depPath.length*i.depPath.length);
			//}else{
				Integer count = posMap.get(a.name());
				if(count != null)
					inst.setValue(a, new Double(count));
				else
					inst.setValue(a, new Double(0));
			//}
		}
		return inst;
	}

	public Instances getInstanceStructure() {		
		return data;
	}

}
